
Partial Class Admin_Codes
    Inherits System.Web.UI.Page
    Dim insertFirstSC As EventHandler()

    Protected Property ServiceCodeGridViewSortDirection() As String
        Get
            If ViewState("ServiceCodeSortDirection") Is Nothing Then
                ViewState("ServiceCodeSortDirection") = SortDirection.Ascending
            End If
            Return CType(ViewState("ServiceCodeSortDirection"), SortDirection)
        End Get
        Set(ByVal value As String)
            ViewState("ServiceCodeSortDirection") = value
        End Set
    End Property

    Protected Property ItemCodeGridViewSortDirection() As String
        Get
            If ViewState("ItemCodeSortDirection") Is Nothing Then
                ViewState("ItemCodeSortDirection") = SortDirection.Ascending
            End If
            Return CType(ViewState("ItemCodeSortDirection"), SortDirection)
        End Get
        Set(ByVal value As String)
            ViewState("ItemCodeSortDirection") = value
        End Set
    End Property

    Protected Property ServiceCodeGridViewSortExpression() As String
        Get
            If ViewState("ServiceCodeSortExpression") Is Nothing Then
                ViewState("ServiceCodeSortExpression") = "ServiceCode"
            End If
            Return ViewState("ServiceCodeSortExpression")
        End Get
        Set(ByVal value As String)
            ViewState("ServiceCodeSortExpression") = value
        End Set
    End Property

    Protected Property ItemCodeGridViewSortExpression() As String
        Get
            If ViewState("ItemCodeSortExpression") Is Nothing Then
                ViewState("ItemCodeSortExpression") = "ItemCode"
            End If
            Return ViewState("ItemCodeSortExpression")
        End Get
        Set(ByVal value As String)
            ViewState("ItemCodeSortExpression") = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load

        ClearStatus()
        SetupJavaScript()
        If Not Page.IsPostBack Then

            Me.BindServiceCodesGrid("ServiceCode")
            Me.BindItemCodesGrid("ItemCode")

        End If

    End Sub

    Private Sub SetServiceCodeStatus(ByVal message As String)
        'Me.MessageLabel.Text = message
        Me.ServiceCodesScrollingGridview.StatusText = message
    End Sub

    Private Sub SetItemCodeStatus(ByVal message As String)
        Me.ItemCodesScrollingGridView.StatusText = message
    End Sub

    Private Sub ClearStatus()
        SetItemCodeStatus("")
        SetServiceCodeStatus("")
    End Sub

    Private Sub SetupJavaScript()
        EntityController.JavaScriptAddChildDiv(Me.pageDiv.ClientID, CInt(70))
        EntityController.JavaScriptSetFixedHeight(30)
    End Sub

    Protected Sub BindServiceCodesGrid(ByVal sortExpression As String)

        If ServiceCodeGridViewSortDirection = SortDirection.Descending Then
            Me.ServiceCodesScrollingGridview.DataSource = Cdm.DataCore.LookupTables.LuServiceCode.ListBySort(sortExpression, False)
        Else
            Me.ServiceCodesScrollingGridview.DataSource = Cdm.DataCore.LookupTables.LuServiceCode.ListBySort(sortExpression, True)
        End If
        Me.ServiceCodesScrollingGridview.DataBind()
        BindCategoryDropDowns()
    End Sub

    Private Function GetCategories() As Generic.List(Of Cdm.DataCore.LookupTables.LuServiceCodeCategory)
        Return Cdm.DataCore.LookupTables.LuServiceCodeCategory.ListAll(0, "Select Category...")
    End Function

    Private Sub BindCategoryDropDowns()
        If ServiceCodesScrollingGridview.EditIndex > -1 Then
            Dim editCategoryDropDownList As DropDownList = Me.ServiceCodesScrollingGridview.Rows(ServiceCodesScrollingGridview.EditIndex).FindControl("EditCategoryDropDownList")
            Dim newCategoryId As Integer = Convert.ToInt32(ServiceCodesScrollingGridview.DataKeys(ServiceCodesScrollingGridview.EditIndex).Item(1))
            If editCategoryDropDownList IsNot Nothing Then
                editCategoryDropDownList.DataSource = GetCategories()
                editCategoryDropDownList.DataTextField = "Description"
                editCategoryDropDownList.DataValueField = "Id"
                editCategoryDropDownList.DataBind()
                editCategoryDropDownList.SelectedValue = newCategoryId.ToString()
            End If
        End If
        If Me.ServiceCodesScrollingGridview.FooterRow IsNot Nothing Then
            Dim addCategoryDropdownList As DropDownList = Me.ServiceCodesScrollingGridview.FooterRow.FindControl("AddCategoryDropDownList")
            If addCategoryDropdownList IsNot Nothing Then
                addCategoryDropdownList.DataSource = GetCategories()
                addCategoryDropdownList.DataTextField = "Description"
                addCategoryDropdownList.DataValueField = "Id"
                addCategoryDropdownList.DataBind()
            End If
        Else
            Dim addCategoryDropdownlist As DropDownList = Me.ServiceCodesScrollingGridview.Controls(0).Controls(0).Controls(0).FindControl("AddCategoryDropdownList")
            addCategoryDropdownlist.DataSource = GetCategories()
            addCategoryDropdownlist.DataTextField = "Description"
            addCategoryDropdownlist.DataValueField = "Id"
            addCategoryDropdownlist.DataBind()
            Dim addButton As Button = Me.ServiceCodesScrollingGridview.Controls(0).Controls(0).Controls(0).FindControl("AddFirstServiceCodeButton")
            AddHandler addButton.Click, AddressOf AddFirstServiceCode
        End If

    End Sub

    Protected Sub AddFirstServiceCode(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim addCategoryDropdownlist As DropDownList = Me.ServiceCodesScrollingGridview.Controls(0).Controls(0).Controls(0).FindControl("AddCategoryDropdownList")
        Dim addDescriptionTextBox As TextBox = Me.ServiceCodesScrollingGridview.Controls(0).Controls(0).Controls(0).FindControl("AddDescriptionTextBox")
        Dim addServiceCodeTextbox As TextBox = Me.ServiceCodesScrollingGridview.Controls(0).Controls(0).Controls(0).FindControl("AddServiceCodeTextBox")
        Dim newServiceCode As String = addServiceCodeTextbox.Text
        Dim newDescription As String = addDescriptionTextBox.Text
        Dim newCategoryId As Integer = Convert.ToInt32(addCategoryDropdownlist.SelectedValue)
        Dim newLuServiceCode As New Cdm.DataCore.LookupTables.LuServiceCode(newServiceCode, newDescription, newCategoryId)
        newLuServiceCode.Persist()
        BindServiceCodesGrid("ServiceCode")
    End Sub

    Protected Sub AddFirstItemCode(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim addDescriptionTextBox As TextBox = Me.ItemCodesScrollingGridView.Controls(0).Controls(0).Controls(0).FindControl("AddDescriptionTextBox")
        Dim addItemCodeTextbox As TextBox = Me.ItemCodesScrollingGridView.Controls(0).Controls(0).Controls(0).FindControl("AddItemCodeTextBox")
        Dim newItemCode As String = addItemCodeTextbox.Text
        Dim newDescription As String = addDescriptionTextBox.Text
        Dim newLuItemCode As New Cdm.DataCore.LookupTables.LuItemCode(newItemCode, newDescription)
        newLuItemCode.Persist()
        BindItemCodesGrid("ItemCode")
    End Sub


    Protected Sub BindItemCodesGrid(ByVal sortExpression As String)

        If ItemCodeGridViewSortDirection = SortDirection.Descending Then
            Me.ItemCodesScrollingGridView.DataSource = Cdm.DataCore.LookupTables.LuItemCode.ListBySort(sortExpression, False)
        Else
            Me.ItemCodesScrollingGridView.DataSource = Cdm.DataCore.LookupTables.LuItemCode.ListBySort(sortExpression, True)
        End If

        Me.ItemCodesScrollingGridView.DataBind()

    End Sub

    Protected Sub ShowMessage(ByVal message As String, _
                              ByVal foreColor As System.Drawing.Color)

        Me.MessageLabel.ForeColor = foreColor
        Me.MessageLabel.Visible = True

    End Sub

    Protected Sub AddFirstServiceCodeButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddFirstServiceCodeButton.Click

    End Sub

    Protected Sub ServiceCodesScrollingGridview_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ServiceCodesScrollingGridview.DataBound
        AccessibilityController.AddImageToSortableGridHeaders(ServiceCodesScrollingGridview)
    End Sub

    Protected Sub ServiceCodesScrollingGridview_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles ServiceCodesScrollingGridview.PageIndexChanging
        Me.ServiceCodesScrollingGridview.PageIndex = e.NewPageIndex
        Me.BindServiceCodesGrid(ServiceCodeGridViewSortExpression)
    End Sub

    Protected Sub ServiceCodesScrollingGridview_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles ServiceCodesScrollingGridview.RowCancelingEdit
        Me.ServiceCodesScrollingGridview.EditIndex = -1
        Me.BindServiceCodesGrid(ServiceCodeGridViewSortExpression)
        SetServiceCodeStatus("Service Code Update Cancelled.")
    End Sub

    Protected Sub ServiceCodesScrollingGridview_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles ServiceCodesScrollingGridview.RowCommand
        If e.CommandName = "Add" Then
            Dim addCategoryDropdown As DropDownList = ServiceCodesScrollingGridview.FooterRow.FindControl("AddCategoryDropdownList")
            Dim categoryId As Integer = Convert.ToInt32(addCategoryDropdown.SelectedValue)
            Dim serviceCode As New Cdm.DataCore.LookupTables.LuServiceCode(CType(Me.ServiceCodesScrollingGridview.FooterRow.Cells(0).FindControl("ServiceCodeFooterTextBox"), TextBox).Text, _
                                                                           CType(Me.ServiceCodesScrollingGridview.FooterRow.Cells(1).FindControl("ServiceDescriptionFooterTextBox"), TextBox).Text, categoryId)
            serviceCode.Persist()
            Me.BindServiceCodesGrid(ServiceCodeGridViewSortExpression)
            Me.ServiceCodesScrollingGridview.PageIndex = Me.ServiceCodesScrollingGridview.PageCount
            SetServiceCodeStatus("Service Code Added.")
        End If
    End Sub

    Protected Sub ServiceCodesScrollingGridview_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles ServiceCodesScrollingGridview.RowEditing
        Me.ServiceCodesScrollingGridview.EditIndex = e.NewEditIndex
        Me.BindServiceCodesGrid(ServiceCodeGridViewSortExpression)
    End Sub

    Protected Sub ServiceCodesScrollingGridview_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles ServiceCodesScrollingGridview.RowUpdated
        SetServiceCodeStatus("Service Code Updated.")
    End Sub

    Protected Sub ServiceCodesScrollingGridview_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles ServiceCodesScrollingGridview.RowUpdating
        Try

            Dim serviceCode As Cdm.DataCore.LookupTables.LuServiceCode
            serviceCode = Cdm.DataCore.LookupTables.LuServiceCode.Retrieve(Me.ServiceCodesScrollingGridview.DataKeys(e.RowIndex).Values(0))
            serviceCode.ServiceCode = CType(Me.ServiceCodesScrollingGridview.Rows(e.RowIndex).FindControl("ServiceCodeTextBox"), TextBox).Text
            serviceCode.Description = CType(Me.ServiceCodesScrollingGridview.Rows(e.RowIndex).FindControl("ServiceDescriptionTextBox"), TextBox).Text
            Dim editCategoryDropdownList As DropDownList = Me.ServiceCodesScrollingGridview.Rows(e.RowIndex).FindControl("EditCategoryDropdownList")
            serviceCode.ServiceCodeCategoryId = Convert.ToInt32(editCategoryDropdownList.SelectedValue)
            serviceCode.Persist()

            Me.ServiceCodesScrollingGridview.EditIndex = -1
            Me.BindServiceCodesGrid(ServiceCodeGridViewSortExpression)
            SetServiceCodeStatus("Service Code Updated.")
        Catch gentleEx As Gentle.Common.GentleException

            e.Cancel = True
            ShowMessage(gentleEx.Message, Drawing.Color.Firebrick)

        Catch ex As Exception

            e.Cancel = True
            ShowMessage(ex.Message, Drawing.Color.Firebrick)

        End Try
    End Sub

    Protected Sub ServiceCodesScrollingGridview_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles ServiceCodesScrollingGridview.Sorting
        If ServiceCodeGridViewSortDirection = SortDirection.Ascending Then
            ServiceCodeGridViewSortDirection = SortDirection.Descending
            Me.BindServiceCodesGrid(e.SortExpression)
        Else
            ServiceCodeGridViewSortDirection = SortDirection.Ascending
            Me.BindServiceCodesGrid(e.SortExpression)
        End If
    End Sub

    Protected Sub ItemCodesScrollingGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ItemCodesScrollingGridView.DataBound
        AccessibilityController.AddImageToSortableGridHeaders(ItemCodesScrollingGridView)
    End Sub

    Protected Sub ItemCodesScrollingGridView_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles ItemCodesScrollingGridView.PageIndexChanging
        Me.ItemCodesScrollingGridView.PageIndex = e.NewPageIndex
        Me.BindItemCodesGrid(ItemCodeGridViewSortExpression)
    End Sub

    Protected Sub ItemCodesScrollingGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles ItemCodesScrollingGridView.RowCancelingEdit
        Me.ItemCodesScrollingGridView.EditIndex = -1
        Me.BindItemCodesGrid(ItemCodeGridViewSortExpression)
    End Sub

    Protected Sub ItemCodesScrollingGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles ItemCodesScrollingGridView.RowCommand
        If e.CommandName = "Add" Then
            Dim itemCode As New Cdm.DataCore.LookupTables.LuItemCode(CType(Me.ItemCodesScrollingGridView.FooterRow.Cells(0).FindControl("ItemCodeFooterTextBox"), TextBox).Text, _
                                                                     CType(Me.ItemCodesScrollingGridView.FooterRow.Cells(2).FindControl("ItemDescriptionFooterTextBox"), TextBox).Text)
            itemCode.Persist()
            Me.BindItemCodesGrid(ItemCodeGridViewSortExpression)
            Me.ItemCodesScrollingGridView.PageIndex = Me.ItemCodesScrollingGridView.PageCount
            SetItemCodeStatus("Item Code Added.")
        End If
    End Sub

    Protected Sub ItemCodesScrollingGridView_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles ItemCodesScrollingGridView.RowEditing
        Me.ItemCodesScrollingGridView.EditIndex = e.NewEditIndex
        Me.BindItemCodesGrid(ItemCodeGridViewSortExpression)
    End Sub

    Protected Sub ItemCodesScrollingGridView_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles ItemCodesScrollingGridView.RowUpdated
        SetItemCodeStatus("Item Code updated.")
    End Sub

    Protected Sub ItemCodesScrollingGridView_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles ItemCodesScrollingGridView.RowUpdating
        Try

            Dim itemCode As Cdm.DataCore.LookupTables.LuItemCode
            itemCode = Cdm.DataCore.LookupTables.LuItemCode.Retrieve(Me.ItemCodesScrollingGridView.DataKeys(e.RowIndex).Values(0))
            itemCode.ItemCode = CType(Me.ItemCodesScrollingGridView.Rows(e.RowIndex).Cells(0).FindControl("ItemCodeTextBox"), TextBox).Text
            itemCode.Description = CType(Me.ItemCodesScrollingGridView.Rows(e.RowIndex).Cells(1).FindControl("ItemDescriptionTextBox"), TextBox).Text
            itemCode.Persist()

            SetItemCodeStatus("Item Code Updated.")
            Me.ItemCodesScrollingGridView.EditIndex = -1
            Me.BindItemCodesGrid(ItemCodeGridViewSortExpression)

        Catch gentleEx As Gentle.Common.GentleException

            e.Cancel = True
            SetItemCodeStatus("Error updating - " + gentleEx.Message)
            'ShowMessage(gentleEx.Message, Drawing.Color.Firebrick)

        Catch ex As Exception

            e.Cancel = True
            'ShowMessage(ex.Message, Drawing.Color.Firebrick)
            SetItemCodeStatus("Error updating - " + ex.Message)
        End Try
    End Sub

    Protected Sub ItemCodesScrollingGridView_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles ItemCodesScrollingGridView.Sorting

        If ItemCodeGridViewSortDirection = SortDirection.Ascending Then
            ItemCodeGridViewSortDirection = SortDirection.Descending
            Me.BindItemCodesGrid(e.SortExpression)
        Else
            ItemCodeGridViewSortDirection = SortDirection.Ascending
            Me.BindItemCodesGrid(e.SortExpression)
        End If
    End Sub
End Class
